package com.bishe.psy.service.impl;

import com.bishe.psy.common.AppResult;
import com.bishe.psy.common.ResultCode;
import com.bishe.psy.config.AppConfig;
import com.bishe.psy.dao.AdmUserMapper;
import com.bishe.psy.exception.ApplicationException;
import com.bishe.psy.model.AdmUser;
import com.bishe.psy.model.ConUser;
import com.bishe.psy.model.StuUser;
import com.bishe.psy.service.IAdmUserService;
import com.bishe.psy.utils.MD5Util;
import com.bishe.psy.utils.StringUtil;
import com.bishe.psy.utils.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: L
 * Date: 2024-03-28
 * Time: 17:32
 */

@Slf4j
@Service
public class AdmUserServiceImpl implements IAdmUserService {

    @Resource
    private AdmUserMapper admUserMapper;

    @Override
    public AdmUser AdmselectByName(String username) {
        if(StringUtil.isEmpty(username)) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return admUserMapper.AdmselectByName(username);
    }

    @Override
    public AdmUser login(String username, String password) {

        //1.查询用户账号是否存在
        AdmUser admUser = AdmselectByName(username);

        if(admUser == null) {
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }

        //2.密码校验
        String salt = admUser.getSalt();
        String ciphertext = MD5Util.md5(password,salt);
        if(!ciphertext.equals(admUser.getPassword())) {
            log.warn(ResultCode.FAILED_LOGIN.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_LOGIN));
        }
        //3.校验成功返回用户信息
        return admUser;
    }

    @Override
    public void modifyAdmInfo(AdmUser admUser) {

        System.out.println("在这理");
        System.out.println(admUser.getId());
        //1.参数校验
        if(admUser == null || admUser.getId() == null || admUser.getId() <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //2.根据当前id查询用户是是否存在
        AdmUser selectAdmUser = admUserMapper.selectByPrimaryKey(admUser.getId());
        System.out.println("在这理2");
        System.out.println(selectAdmUser);


        //3.判断当前用户是否存在
        if(selectAdmUser == null || selectAdmUser.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }

        //4.创建一个更新对象
        AdmUser updateAdmUser = new AdmUser();
        updateAdmUser.setId(selectAdmUser.getId());


        //5.判断是否能够调用更新语句的字段
        boolean flag = false;

        //6.当昵称字段被修改，则可以调用数据库修改语句
        //当上述字段都没有被修改时，则不能掉用数据库语句
        if(!StringUtil.isEmpty(admUser.getNickname()) && !admUser.getNickname().equals(selectAdmUser.getNickname())) {
            updateAdmUser.setNickname(admUser.getNickname());
            flag = true;
        }

        //7.判断是否可以调用数据库
        if(flag == false) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        System.out.println(updateAdmUser);
        //8.调用数据库
        int row = admUserMapper.updateByPrimaryKeySelective(updateAdmUser);
        System.out.println("row = " + row);

        //9.判断是否更新成功
        if(row != 1) {
            log.warn(ResultCode.FAILED_CREATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
    }

    @Override
    public AdmUser selectByPrimaryKey(Long id) {

        //1.参数校验
        if(id == null || id <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //2.调用数据库
        AdmUser admUser = admUserMapper.selectByPrimaryKey(id);

        //3.返回数据
        return admUser;
    }

    @Override
    public void modifyPassword(Long id, String oldPassword, String newPassword) {
        //1.参数校验
        if(id == null || id <= 0 || StringUtil.isEmpty(oldPassword) || StringUtil.isEmpty(newPassword)) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //2.根据id查询用户信息
        AdmUser admUser = admUserMapper.selectByPrimaryKey(id);

        //3.判断用户是否存在
        if(admUser == null || admUser.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }

        //4.判断输入的旧密码是否与表中保存的一致
        String oldEncryptPassword = MD5Util.md5(oldPassword,admUser.getSalt());
        if(!oldEncryptPassword.equalsIgnoreCase(admUser.getPassword())) {
            log.warn(ResultCode.FAILED_OLD_PASSWORD.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_OLD_PASSWORD));
        }

        //5.对新密码进行MD5加密
        String newSalt = UUIDUtil.UUID_32();
        String encryptPassword = MD5Util.md5(newPassword,newSalt);

        //6.更新修改内容
        AdmUser updateAdm = new AdmUser();
        updateAdm.setId(admUser.getId());
        updateAdm.setSalt(newSalt);
        updateAdm.setPassword(encryptPassword);
        Date date = new Date();
        updateAdm.setUpdateState(date);

        //7.调用数据库
        int row = admUserMapper.updateByPrimaryKeySelective(updateAdm);
        if(row != 1) {
            log.warn(ResultCode.FAILED_CREATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
    }

}
